package day52.org.controller;

import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import day52.org.domain.Employee;
import day52.org.query.EmployeeQuery;
import day52.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//Restful风格
//@GetMapping      简单查询
//@PostMapping     高级查询
//@PutMapping      新增更新数据
//@DeleteMapping   删除数据
//@PatchMapping    批量操作


@RestController
@RequestMapping("/emp")
@Api(tags = "员工接口")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value = "员工查找",notes = "根据员工id查找")
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable Long id){
        return employeeService.findOne(id);
    }

    @ApiOperation(value = "员工查找",notes = "查找所有员工")
    @GetMapping
    public List<Employee> findAll(){
       return employeeService.findAll();
    }

    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult save(@RequestBody Employee employee){
        if(employee.getId() == null){
            employeeService.add(employee);
        }else{
            employeeService.update(employee);
        }
        return AjaxResult.me();
    }

    @ApiOperation(value = "员工删除",notes = "根据员工id进行删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me();
    }
    @ApiOperation(value = "高级查询",notes = "进行分页等高级查询")
    @PostMapping
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.pageQuery(employeeQuery);
    }

    @ApiOperation(value = "批量删除",notes = "根据员工id进行删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        employeeService.patchDelete(ids);
        return AjaxResult.me();
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> findEmpByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.findEmpByShopId(shopId);
    }
}
